﻿
Partial Class DetailKhachSanPut
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Request.QueryString("IDKS") Is Nothing Or Session("IDCity") Is Nothing Then
            Response.Redirect("~/index.aspx")
        End If
        Dim arrivalDate As Date = Nothing
        'Dim departureDate As Date = Session("DepartureDate")
        If Session("ArrivalDate") IsNot Nothing AndAlso Not Session("ArrivalDate").Equals("") Then
            arrivalDate = Session("ArrivalDate")
        Else
            arrivalDate = Now.Date
        End If
        For i As Integer = 0 To 60
            Dim _mDate As Date = DateAdd(DateInterval.Day, +i, arrivalDate)
            Dim valueDay As String = _mDate
            Dim showDate As String = String.Format("{0}/{1}/{2}", _mDate.Day, _mDate.Month, _mDate.Year)
            Dim textDay As String = String.Format("{0} {1}", Util.GetThuTrongTuan(CInt(_mDate.DayOfWeek), "Chủ Nhật"), showDate)
            Dim item As New ListItem(textDay, valueDay)
            txtCheckIn.Items.Add(item)
        Next
        ''            
        SetOptionForControlSelect(10, txtNights)
        SetOptionForControlSelect(20, txtRooms)
        SetOptionForControlSelect(5, txtExtraBeds)
        If Not IsPostBack Then
            'DetailKhachSan.aspx?IDKS={0}
            Dim khachSan As KhachSan = Nothing
            If Request.QueryString("IDKS") IsNot Nothing Then
                Dim idKS As New Guid(Request.QueryString("IDKS"))
                khachSan = KhachSanServer.GetKhachSanByIDKhachSan(idKS)
                'Dim strCity As String = KhachSanServer.GetCityNameByIDCity(khachSan.CITY_ID)

                divNameHotel.InnerHtml = khachSan.NAME
                lnkQuayLai.Text = "Khách Sạn tại " + Session("NameCity")
                Dim strURLQuayLai As String = String.Format("~/SearchKhachSan.aspx?ArrivalDate={0}&DepartureDate={1}&CityID={2}&Guests={3}&Rooms={4}", Session("ArrivalDate"), Session("DepartureDate"), Session("IDCity"), Session("Guests"), Session("Rooms"))
                lnkQuayLai.PostBackUrl = strURLQuayLai

                Dim nameLoaiPhong As String = ""
                Dim strPrice As String = ""
                Dim khuyenMai As New KhuyenMaiTheoHotel
                Dim loaiPhongKS As New KhachSan
                If Request.QueryString("IDLP") IsNot Nothing Then
                    Dim idLP As New Guid(Request.QueryString("IDLP"))
                    loaiPhongKS = KhachSanServer.GetLoaiHotelByIdLoaiPhong(idKS, idLP)
                    nameLoaiPhong = loaiPhongKS.NAMELOAIPHONG
                    If loaiPhongKS.HIENTHIGIA = True Then
                        strPrice = "<div id='divShowPrice'>"
                        strPrice &= "<div class='rate' style='margin-top: 10px;'>" + Format(loaiPhongKS.GIA, "#,###") + "</div>"
                        strPrice &= "<div style='font-weight: normal; font-size: .8em; color: #999;'> VND/đêm</div></div>"
                    Else
                        strPrice = "<div id='divHidePriceDetails' class='top-15 bottom-15'>"
                        Dim strURLLayGia As String = String.Format("GetPrice.aspx?IDKS={0}&IDLP={1}", Request.QueryString("IDKS"), loaiPhongKS.IDLOAIPHONG)
                        strPrice &= " <a class='ui-link' data-ajax='false' rel='nofollow' href='" + strURLLayGia + "' style='text-decoration:none;'> "
                        strPrice &= "<label class='ui-corner-all label_clicklaygia'>Click lấy giá</label></a></div>"
                    End If
                End If
                If Request.QueryString("IDKM") IsNot Nothing Then
                    Dim idKM As New Guid(Request.QueryString("IDKM"))
                    khuyenMai = KhuyenMaiTheoHotelServer.GetKhuyenMaiByIDKhuyenMai(idKS, idKM)
                    nameLoaiPhong = khuyenMai.NAME_KM
                    If khuyenMai.HIENTHIGIA = True Then
                        strPrice = "<div id='divShowPrice'>"
                        strPrice &= "<div class='rate' style='margin-top: 10px;'>" + Format(khuyenMai.GIA_RE, "#,###") + "</div>"
                        strPrice &= "<div style='font-weight: normal; font-size: .8em; color: #999;'> VND/đêm</div></div>"
                    Else
                        strPrice = "<div id='divHidePriceDetails' class='top-15 bottom-15'>"
                        Dim strURLLayGia As String = String.Format("GetPrice.aspx?IDKS={0}&IDKM={1}", Request.QueryString("IDKS"), khuyenMai.IDKHUYENMAI)
                        strPrice &= " <a class='ui-link' data-ajax='false' rel='nofollow' href='" + strURLLayGia + "' style='text-decoration:none;'> "
                        strPrice &= "<label class='ui-corner-all label_clicklaygia'>Click lấy giá</label></a></div>"
                    End If
                End If
                idLoaiPhong.InnerHtml = nameLoaiPhong
                divPrice.InnerHtml = strPrice

                Dim listLoaiPhong As List(Of KhachSan) = KhachSanServer.GetLoaiHotelByIdHotel(idKS)
                lnkDatPhong.PostBackUrl = String.Format("~/DetailKhachSanPut.aspx?IDKS={0}&IDLP={1}", Request.QueryString("IDKS"), listLoaiPhong(0).IDLOAIPHONG)

            End If

            'Dim strPathAndQuery As String = HttpContext.Current.Request.Url.PathAndQuery
            'Dim strUrl As String = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "")
            'strUrl = strUrl & khachSan.LINK
        End If
    End Sub

    Protected Sub lnkBooking_Click(sender As Object, e As EventArgs) Handles lnkBooking.Click
        Try
            ' DatPhongServer.InsertKhachHangMoi(txtCustomerName.Text, txtEmail.Text, txtMobile.Text)
            '(ByVal idKS As Guid, ByVal fromDate As Date, ByVal toDate As Date, ByVal ghiChu As String, ByVal idLoaiPhong As Guid, ByVal soLuongPhong As Integer, ByVal soGP As Integer,
            'ByVal sale_Price As Double , ByVal  price As Double, ByVal maBookingIDENTITY As Integer,
            'ByVal nameKH As String, ByVal email As String, ByVal phone As String)

            Dim idLP As Guid = Nothing
            Dim idKM As Guid = Nothing
            Dim idKS As New Guid(Request.QueryString("IDKS"))
            Dim fromDate As Date = CDate(txtCheckIn.Value)
            Dim toDate As Date = DateAdd(DateInterval.Day, +txtNights.Value, fromDate)
            Dim khuyenMai As New KhuyenMaiTheoHotel
            Dim loaiPhongKS As New KhachSan
            Dim price As Double = 0
            Dim price_KS As Double = 0
            If Request.QueryString("IDLP") IsNot Nothing Then
                idLP = New Guid(Request.QueryString("IDLP"))
                loaiPhongKS = KhachSanServer.GetLoaiHotelByIdLoaiPhong(idKS, idLP)
                price = loaiPhongKS.GIA
                price_KS = loaiPhongKS.GIAKS
            End If
            If Request.QueryString("IDKM") IsNot Nothing Then
                idKM = New Guid(Request.QueryString("IDKM"))
                khuyenMai = KhuyenMaiTheoHotelServer.GetKhuyenMaiByIDKhuyenMai(idKS, idKM)
                idLP = khuyenMai.IDLOAIPHONG
                price = khuyenMai.GIA_RE
                price_KS = khuyenMai.GIA_KHACH_SAN
            End If
            Dim maBookingNext As Integer = DatPhongServer.GetMaBookingTop() + 1
            DatPhongServer.DatPhong(idKS, fromDate, toDate, txtBookingNotes.InnerText, idLP, txtRooms.Value, txtExtraBeds.Value, price, price_KS, maBookingNext, txtCustomerName.Text, txtEmail.Text, txtMobile.Text, idKM)

            Dim maBooking As String = String.Format("KS24000{0}", maBookingNext)
            Dim htmLValue As String = ""
            Dim subject As String = ""
            If DatPhongServer.IsExistsTrip(maBooking) Then              
                If DatPhongServer.IsExistsMaBookingKhuyenMai(maBooking) Then
                    subject = "Phiếu đặt khuyến mãi khách sạn - Khách Sạn 24h"
                    htmLValue = ReportService.getHTMLFromDatabase(System.Web.HttpContext.Current.Server.MapPath("~/ContnetSendMailBooking.htm"), maBooking, True)
                Else
                    subject = "Phiếu đặt phòng - Khách sạn 24 giờ"
                    'Dim strPathAndQuery As String = HttpContext.Current.Request.Url.PathAndQuery
                    'Dim strUrl As String = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "")
                    'strUrl = strUrl & Request.ApplicationPath
                    ''System.Web.HttpContext.Current.Server.MapPath("~/SendMailDatPhong.html")
                    htmLValue = ReportService.getHTMLFromDatabase(System.Web.HttpContext.Current.Server.MapPath("~/SendMailDatPhong.html"), maBooking, False)
                End If
                If Not htmLValue.Equals("") Then
                    ''send email
                    Dim strEmailCC As String = ""
                    Dim eMail As String = ""
                    If eMail.Equals("") Then
                        eMail = txtEmail.Text
                    Else
                        strEmailCC = String.Format("{0},{1}", strEmailCC, txtEmail.Text)
                    End If
                    Dim errMess As String = Util.SendMail(strEmailCC, eMail, subject, htmLValue)
                End If
                ''
                Dim urL As String = String.Format("~/PutRoomsSuccess.aspx?IDKS={0}&IDBooking={1}", Request.QueryString("IDKS"), maBooking)
                Response.Redirect(urL)
            End If
         
        Catch ex As Exception
            ''Throw ex
        End Try
    End Sub
    Private Sub SetOptionForControlSelect(ByVal num As Integer, ByVal conTrol As HtmlSelect)
        For i As Integer = 1 To num
            Dim item As New ListItem(i, i)
            conTrol.Items.Add(item)
        Next
    End Sub

  
End Class
